/* The objective is to minimize the sum of inversion numbers of a sequences of
 * pairs (a_i, b_i). The inversion number of a sequence { a_i } is defined as
 *
 *     inv(A) = sum # { a_i > a_j, i < j } for j = 1 to n.
 *
 * First, we assert that if a sequence { (a_i, b_i) | i = 1..n } is optimal, 
 * then the sub-sequence { (a_i, b_i) | i = 1..n-1 } must be optimal. This is 
 * because the inversion number contributed by the last pair (a_n, b_n) is not
 * affected by how the first (n-1) terms are arranged. 
 *     
 * Next we show that moving a[i] by one position forward (i.e. swapping a[i]
 * and a[i+1]) can change the inversion number at most by one. This follows 
 * the same reasoning as above: swapping two consecutive elements does not 
 * affect the inversion number contributed from the rest elements.
 *
 * Now we arrange the sequence of pairs in lexical order (a_i, b_i). That is,
 * we first sort the pairs by a_i; on a range where the a[i]'s are equal, we 
 * sort the range by b[i]. We assert that such sequence, S, has the smallest
 * inversion number of all sequences of length n. We show this by induction.
 * When there are two pairs, its optimality is obvious. Now take an arbitrary
 * optimal sequence S' of length n. If the last element in S' and S are the
 * same, then
 *
 *     inv(S) = inv(S[1..n-1]) + inv(S[n]) 
 *           <= inv(S'[1..n-1]) + inv(S'[n]) 
 *            = inv(S'). 
 *
 * Now suppose the last element in S' and S are different. Let S'[n] = S[i].
 * We can swap S[i] with each element to its right in turn to move it to the
 * end of S. During this process the inversion number of S does not decrease.
 * The resulting sequence, S*, has its first (n-1) elements sorted in lexical
 * order and its last element equal to S'[n]. Therefore
 *
 *     inv(S) <= inv(S*) = inv(S*[1..n-1]) + inv(S*[n])
 *                      <= inv(S'[1..n-1]) + inv(S'[n])
 *                       = inv(S').
 *
 * So we're left to compute the inversion number of the sequence { b_i } in 
 * the lexically sorted sequence S. This is a standard algorithm which can be
 * implemented as a by-product of a merge-sort procedure.
 */

#include <stdio.h>
#include <algorithm>
#include <functional>
#if 0
#include "sort.hpp"
#else
/* Sorts an array of elements in-place according to the predicate 'less'.
 * Returns the inversion number as a by-product. The sorting is stable.
 */
template <class Ty, class Pred, class Counter>
Counter insertion_sort(Ty dest[], size_t n, Pred less, Counter inv_num)
{
    for (size_t i = 1; i < n; i++)
    {
        Ty val = dest[i];
        size_t j;
        for (j = i; j > 0 && less(val, dest[j-1]); j--)
        {
            dest[j] = dest[j-1];
        }
        if (j != i)
        {
            dest[j] = val;
            inv_num += (i - j);
        }
    }
    return inv_num;
}

/* Merges two arrays according to predicate 'less' and stores the result in
 * a separate array. Returns the inversion number as a by-product.
 */
template <class FwdIt1, class FwdIt2, class OutIt, class Pred, class Counter>
Counter merge(FwdIt1 first1, FwdIt1 last1, FwdIt2 first2, FwdIt2 last2, 
              OutIt dest, Pred less, Counter inv_num)
{
    FwdIt1 it1 = first1;
    FwdIt2 it2 = first2;
    OutIt it = dest;
    size_t inv_cnt = 0;
    while (it1 != last1 && it2 != last2)
    {
        if (less(*it2, *it1))
        {
            *it = *it2;
            ++it2;
            inv_cnt++;
        }
        else
        {
            *it = *it1;
            ++it1;
            inv_num += inv_cnt;
        }
        ++it;
    }
    for (; it1 != last1; ++it1, ++it)
    {
        *it = *it1;
        inv_num += inv_cnt;
    }
    for (; it2 != last2; ++it2, ++it)
    {
        *it = *it2;
    }
    return inv_num;
}

/* Sorts an array of elements in-place according to the predicate 'less'. An
 * auxiliary buffer of the same size as data must be provided. The contents
 * of the auxiliary buffer upon return is undefined. The sorting is stable.
 * Returns the inversion number as a by-product.
 */
template <class Ty, class Pred, class Counter>
Counter merge_sort(Ty data[], size_t n, Pred less, Ty aux[], Counter num_inv)
{
    /* Use insertion sort to sort small blocks in-place. */
    const size_t BlockSize = 4;
    for (size_t i = 0; i < n; i += BlockSize)
    {
        size_t m = std::min(BlockSize, n - i);
        num_inv = insertion_sort(data + i, m, less, num_inv);
    }

    /* Recursively merge small blocks. */
    Ty *input = data;
    Ty *output = aux;
    for (size_t width = BlockSize; width < n; width *= 2)
    {
        for (size_t i = 0; i < n; i += width * 2)
        {
            Ty *start = input + i;
            Ty *mid = input + std::min(i + width, n);
            Ty *end = input + std::min(i + width * 2, n);
            Ty *out = output + i;
            if (mid == end)
                std::copy(start, mid, out);
            else
                num_inv = merge(start, mid, mid, end, out, less, num_inv);
        }
        std::swap(input, output);
    }

    /* Make sure we output to the data[] buffer. */
    std::swap(input, output);
    if (output != data)
        std::copy(output, output + n, data);

    return num_inv;
}
#endif

#define MAX_N 100000

struct pair_t
{
    int a, b;
    bool operator < (const pair_t &other) const
    {
        return (a < other.a) || (a == other.a && b < other.b);
    }
};

union
{
    pair_t S[MAX_N];
    int arr[MAX_N*2];
} g;

int main()
{
    int n;
    while (scanf("%d", &n) == 1 && n > 0)
    {
        for (int i = 0; i < n; i++)
            scanf("%d %d", &g.S[i].a, &g.S[i].b);

        std::sort(g.S + 0, g.S + n);

        for (int i = 0; i < n; i++)
            g.arr[i] = g.S[i].b;

        unsigned long long inv_num = 
            merge_sort(g.arr, n, std::less<int>(), g.arr + n, 0ULL);
        printf("%llu\n", inv_num);
    }
    return 0;
}
